package com.cutefool.zero.code.core.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class Dbms extends BasicsDomain<Dbms> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("CODE_")
    private String code;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("NAME_")
    private String name;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("SYNC_MODE_")
    private Byte syncMode;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("SOURCE_")
    private String source;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("APP_ID_")
    private Long appId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COMPANY_ID_")
    private Long companyId;

    private static final Dbms instance = new Dbms();

    private static final Table table = new Table();

    public static Dbms instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<Dbms>> insert() {
        return (InsertDSL<Dbms> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.code).toProperty("code");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.syncMode).toProperty("syncMode");
            dsl.map(table.source).toProperty("source");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<Dbms>> insertMultiple() {
        return (MultiRowInsertDSL<Dbms> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.code).toProperty("code");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.syncMode).toProperty("syncMode");
            dsl.map(table.source).toProperty("source");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.code).equalTo(this::getCode)
           .set(table.name).equalTo(this::getName)
           .set(table.syncMode).equalTo(this::getSyncMode)
           .set(table.source).equalTo(this::getSource)
           .set(table.appId).equalTo(this::getAppId)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt)
           .set(table.companyId).equalTo(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.code).equalToWhenPresent(this::getCode)
           .set(table.name).equalToWhenPresent(this::getName)
           .set(table.syncMode).equalToWhenPresent(this::getSyncMode)
           .set(table.source).equalToWhenPresent(this::getSource)
           .set(table.appId).equalToWhenPresent(this::getAppId)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt)
           .set(table.companyId).equalToWhenPresent(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.code, table.name, table.syncMode, table.source, table.appId, table.createBy, table.createAt, table.updateBy, table.updateAt, table.companyId};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(11);
        maps.put("id", table.id);
        maps.put("code", table.code);
        maps.put("name", table.name);
        maps.put("syncMode", table.syncMode);
        maps.put("source", table.source);
        maps.put("appId", table.appId);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        maps.put("companyId", table.companyId);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<String> code = column("CODE_", JDBCType.VARCHAR);

        public final SqlColumn<String> name = column("NAME_", JDBCType.VARCHAR);

        public final SqlColumn<Byte> syncMode = column("SYNC_MODE_", JDBCType.TINYINT);

        public final SqlColumn<String> source = column("SOURCE_", JDBCType.VARCHAR);

        public final SqlColumn<Long> appId = column("APP_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> createBy = column("CREATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> companyId = column("COMPANY_ID_", JDBCType.BIGINT);

        public Table() {
            super("CUTE_DBMS", Table::new);
        }
    }
}